package de.jmda.mview;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import de.jmda.gui.swing.mvc.ControllerContextProvider;
import de.jmda.gui.swing.mvc.ControllerSupportDisabled;
import de.jmda.gui.swing.mvc.ControllerSupportEnabled;

@ControllerSupportEnabled
public class MView extends JFrame implements ControllerContextProvider
{
	@ControllerSupportDisabled
	private static final long serialVersionUID = 1L;
	
	private static class Runnable implements java.lang.Runnable
	{
		private String projectRepositoryFileName;

		private Runnable(String[] args)
		{
			if (args.length > 0)
			{
				projectRepositoryFileName = args[0];
			}
		}

		@Override
    public void run()
    {
			try
			{
				MView frame = new MView(projectRepositoryFileName);
				frame.setVisible(true);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
    }
	}
	
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenuItem mntmOpen;
	private JMenuItem mntmSave;
	private JSeparator mnFileSeparator;
	private JMenuItem mntmExit;
	private JMenuItem mntmNew;

//  private JPanel contentPane;

	/**
	 * Launch the application.
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//		UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
		EventQueue.invokeLater(new Runnable(args));
	}

	/**
	 * Create the frame.
	 */
	public MView(String projectRepositoryFileName)
	{
		controller.updateProjectRepository(projectRepositoryFileName);

		// initialise view (component container)
		initialize();
	}

	private void initialize()
	{
		setMinimumSize(new Dimension(800, 600));
		setPreferredSize(new Dimension(800, 600));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(Frame.MAXIMIZED_BOTH);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		mntmNew = new JMenuItem("New");
		mntmNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				controller.updateProjectRepository(null);
			}
		});
		mnFile.add(mntmNew);

		mntmOpen = new JMenuItem("Open");
		mnFile.add(mntmOpen);
		
		mntmSave = new JMenuItem("Save");
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				controller.saveRepositoryActionPerformed(evt);
			}
		});
		mnFile.add(mntmSave);
		
		mnFileSeparator = new JSeparator();
		mnFile.add(mnFileSeparator);
		
		mntmExit = new JMenuItem("Exit");
		mnFile.add(mntmExit);
	}

	//GEN-BEGIN:controller support                                                                                   
	@de.jmda.gui.swing.mvc.ControllerSupportDisabled                                                 
	private de.jmda.mview.MViewController controller =                                                   
			new de.jmda.mview.MViewController(this);                                                         
                                                                                                    
	/**                                                                                                
	 * @see de.jmda.gui.swing.mvc.ControllerContextProvider#getControllerContext()                   
	 */                                                                                                
	@Override // make sure that this class implements de.jmda.gui.swing.mvc.ControllerContextProvider
	public de.jmda.gui.swing.mvc.ControllerContext getControllerContext()                            
	{                                                                                                  
		return new de.jmda.mview.MViewControllerContext(
				this,
				menuBar,
				mnFile,
				mntmOpen,
				mntmSave,
				mnFileSeparator,
				mntmExit,
				mntmNew);                                                         
	}                                                                                                  
	//GEN-END:controller support
}